<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="手机品牌：">
            <el-select
              v-model="listQuery.brandId"
              placeholder="请选择品牌"
              clearable
            >
              <el-option
                v-for="item in brandOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="输入搜索：">
            <el-input
              style="width: 203px"
              v-model="listQuery.keyword"
              placeholder="手机名称"
            ></el-input>
          </el-form-item>

          <el-form-item label="发售日期：">
            <el-date-picker
              v-model="time"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              @change="changeTime"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table
        ref="brandTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="排行" type="index" width="100" align="center">
        </el-table-column>
         <el-table-column label="品牌" width="150" align="center">
          <template slot-scope="scope">
            <img style="height: 80px" :src="scope.row.logo" />
          </template>
        </el-table-column>
        <el-table-column label="手机名称" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.productName }}</template>
        </el-table-column>
        <el-table-column label="发售日期" width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.saleTime }}
          </template>
        </el-table-column>

        <el-table-column label="电池容量" align="center">
          <template slot-scope="scope">
            <el-progress
              :text-inside="true"
              :stroke-width="24"
              :percentage="scope.row.batteryCapacity / scope.row.per * 100"
              :format="format(scope.row.batteryCapacity)"
            >
            </el-progress>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { searchList } from "@/api/basic";
import { fetchList as fetchBrandList } from "@/api/brand";

const defaultListQuery = {
  brandId: null,
  keyword: null,
  startTime: "",
  endTime: "",
  pageNum: 1,
  pageSize: 10,
  order: null
};

export default {
  name: "tab2",
  data() {
    return {
      list: null,
      total: null,
      listLoading: true,
      brandOptions: [],
      time: [],
      listQuery: Object.assign({}, defaultListQuery),
      pickerOptions: {
        shortcuts: [
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30 * 3);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近半年",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30 * 6);
              picker.$emit("pick", [start, end]);
            },
          },
                    {
            text: "最近一年",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30 * 12);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    // 品牌下拉初始化
    this.getBrandList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      this.listQuery.order = "tab2";
      searchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    getBrandList() {
      fetchBrandList({ pageNum: 1, pageSize: 100 }).then((response) => {
        this.brandOptions = [];
        let brandList = response.data.list;
        for (let i = 0; i < brandList.length; i++) {
          this.brandOptions.push({
            label: brandList[i].brandName,
            value: brandList[i].id,
          });
        }
      });
    },

    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.time = [];
    },
    // 分页
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },

    // 控制进度条显示的文本
    format(batteryCapacity) {
      return () => {
          return `${batteryCapacity}mAh`;
      }
    },

    // 时间选择器参数变化时，给startTime、endTime赋值
    changeTime(val) {
      if (val) {
        this.listQuery.startTime = val[0];
        this.listQuery.endTime = val[1];
      } else {
        this.listQuery.startTime = "";
        this.listQuery.endTime = "";
      }
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>


